class ApplicationController < ActionController::Base
	protect_from_forgery
	
	include ApplicationHelper
	include SessionsHelper
	
	rescue_from 'SecurityTransgression', with: :render_403
	
	before_filter :set_locale
	before_filter :store_location	# Saves location to redirect back to it when login necessary
    before_filter :sidebar_menu
	before_filter :require_logged_in
	before_filter :enforce_access_control
	# All filter after the enforce_access_control filter won't be processed when user has no permission
 
	def set_locale()
		unless current_user.preferred_locale.blank?
			I18n.locale = current_user.preferred_locale 
		else
			I18n.locale = params[:locale]
		end
	end
    
    def sidebar_menu
        @latest_created_documents = latest_created_documents
    end
	
	def default_url_options(options={})
		{ :locale => I18n.locale }
	end
	
	private
	
		def render_403(exception)
			respond_to do |type| 
				type.html { render :template => "errors/403", :status => :forbidden } 
				type.all  { render :nothing => true, :status => :forbidden } 
			end
			true
		end

end
